package com.tanhua.appserver.interceptor;

import com.tanhua.model.pojo.User;

public class UserHolder {

    private static ThreadLocal<User> userThreadLocal = new ThreadLocal<User>();

    /**
     * 保存数据到线程
     */
    public static void set(User user) {
        userThreadLocal.set(user);
    }

    /**
     * 获取线程中的用户信息
     */
    public static User get() {
        return userThreadLocal.get();
    }


    /**
     * 从当前线程，获取用户对象的id
     */
    public static Long getUserId() {
        if (userThreadLocal.get() == null) {
            return null;
        }
        return userThreadLocal.get().getId();
    }

    /**
     * 从当前线程，获取用户对象的手机号码
     */
    public static String getMobile() {
        if (userThreadLocal.get() == null) {
            return null;
        }
        return userThreadLocal.get().getMobile();
    }

     /**
     * 移除线程中数据
     */
    public static void remove() {
        userThreadLocal.remove();
    }
}